<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/banner.css">
</head>

<body>
    <div class="mybanner">
        <ul class="hot">
            <li>
                <a href=""> <img src="images/banner1.jpg" alt=""> </a>
            </li>
            <li>
                <a href="#"> <img src="images/banner2.jpg" alt=""> </a>
            </li>
            <li>
                <a href="#"> <img src="images/banner3.jpg" alt=""> </a>
            </li>
            <li>
                <a href="#"> <img src="images/banner4.jpg" alt=""> </a>
            </li>
        </ul>
        <div class='arrow'>
            <span class="prev"> < </span>
            <span class="next"> > </span>
        </div>
        <ul class="circle-list">
        </ul>
    </div>
    <script>
        window.addEventListener('load', function() {
            var banner = document.querySelector('.mybanner');
            var arrow = document.querySelector('.arrow');
            var hot = document.querySelector('.hot');
            var list = document.querySelector('.circle-list');
            var pre = document.querySelector('.prev');
            var next = document.querySelector('.next');
            var num = 0;
            var circle = 0;
            banner.addEventListener('mouseover', function() {
                arrow.style.display = 'block';
                clearInterval(timer);
                timer = null;
            })
            banner.addEventListener('mouseout', function() {
                arrow.style.display = 'none';
                timer = setInterval(function() {
                    next.click();
                }, 2000)
            })
            for (var i = 0; i < hot.children.length; i++) {
                var li = document.createElement('li');
                li.className = 'circle';
                li.setAttribute('data-n', i)
                list.append(li);
            }

            function animate(obj, target, callback) {
                clearInterval(obj.time);
                obj.time = setInterval(function() {
                    var step = (target - obj.offsetLeft) / 10;
                    step = step > 0 ? Math.ceil(step) : Math.floor(step);
                    if (obj.offsetLeft == target) {
                        clearInterval(obj.time);
                        callback && callback();
                    }
                    obj.style.left = obj.offsetLeft + step + 'px';
                }, 30);
            }
            list.children[0].className = 'circle active';
            for (var i = 0; i < list.children.length; i++) {
                list.children[i].addEventListener('click', function() {
                    for (var i = 0; i < list.children.length; i++) {
                        list.children[i].className = 'circle';
                    }
                    this.className = 'circle active';
                    num = this.getAttribute('data-n');
                    circle = this.getAttribute('data-n');
                    animate(hot, -num * banner.offsetWidth);
                })
            }
            var first = hot.children[0].cloneNode(true);
            hot.appendChild(first);
            var flag = true;
            next.addEventListener('click', function() {
                if (flag) {
                    flag = false;
                    if (num == hot.children.length - 1) {
                        num = 0;
                        hot.style.left = '0px';
                    }
                    num++;
                    animate(hot, -num * banner.offsetWidth, function() {
                        flag = true;
                    });
                    circle = ++circle % (hot.children.length - 1);
                    for (var i = 0; i < list.children.length; i++) {
                        list.children[i].className = 'circle';
                    }
                    list.children[circle].className = 'circle active';
                }
            })
            pre.addEventListener('click', function() {
                if (flag) {
                    flag = false;
                    if (num == 0) {
                        num = 4;
                        hot.style.left = -num * banner.offsetWidth + 'px';
                    }
                    num--;
                    animate(hot, -num * banner.offsetWidth, function() {
                        flag = true;
                    });
                    --circle;
                    if (circle == -1) {
                        circle = hot.children.length - 2;
                    }
                    for (var i = 0; i < list.children.length; i++) {
                        list.children[i].className = 'circle';
                    }
                    list.children[circle].className = 'circle active';
                }
            })
            var timer = setInterval(function() {
                next.click();
            }, 2000)
        })
    </script>
</body>

</html>